<?php
App::uses('AppController', 'Controller');
/**
 * Occours Controller
 *
 * @property Occour $Occour
 */
class OccoursController extends AppController {

	public $uses = array('Occour','Team','Match','Player');
/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Occour->recursive = 0;
		$this->set('occours', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Occour->exists($id)) {
			throw new NotFoundException(__('Invalid occour'));
		}
		$options = array('conditions' => array('Occour.' . $this->Occour->primaryKey => $id));
		$this->set('occour', $this->Occour->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add($game = null) {
		if ($this->request->is('post')) {
			$this->Occour->create();
			if ($this->Occour->save($this->request->data)) {
				$this->Session->setFlash(__('The occour has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The occour could not be saved. Please, try again.'));
			}
		}
		
		if ($game == null){
			$matches = $this->Occour->Match->find('list');
			$players = $this->Occour->Player->find('list');
		}
		else {
			$aux = $this->Match->findById($game);
			$matches = $this->Match->find('list', array('conditions' => 'Match.id = '.$game.''));
			$options['conditions'] = array(
					'Player.team_id = '.$aux['Match']['home'].' OR Player.team_id = '.$aux['Match']['visitor'].''
				);
			$players = $this->Player->find('list',$options);
		}
		
		$events = $this->Occour->Event->find('list');
		$this->set(compact('matches', 'players', 'events'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Occour->exists($id)) {
			throw new NotFoundException(__('Invalid occour'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Occour->save($this->request->data)) {
				$this->Session->setFlash(__('The occour has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The occour could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Occour.' . $this->Occour->primaryKey => $id));
			$this->request->data = $this->Occour->find('first', $options);
		}
		$matches = $this->Occour->Match->find('list');
		$players = $this->Occour->Player->find('list');
		$events = $this->Occour->Event->find('list');
		$this->set(compact('matches', 'players', 'events'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Occour->id = $id;
		if (!$this->Occour->exists()) {
			throw new NotFoundException(__('Invalid occour'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Occour->delete()) {
			$this->Session->setFlash(__('Occour deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Occour was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
}
